package com.itheima.controller;

import com.itheima.constant.MessageConstant;
import com.itheima.entity.Result;
import com.itheima.pojo.Member;
import com.itheima.service.MemberService;
import com.itheima.service.UserService;
import jdk.nashorn.internal.ir.annotations.Reference;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import redis.clients.jedis.JedisPool;

import javax.servlet.ServletException;
import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;


/**
 * @author l
 * @date 2020-08-21 17:09
 */

@RestController
@RequestMapping("/user")
public class UserController {

    @Reference
    private UserService userService;
    @Autowired
    private JedisPool jedisPool;

    @RequestMapping("/message")
    public Result findUserMessage(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        String memberMessage =null;
        Cookie[] cookies = request.getCookies();
        if (cookies==null||cookies.length<=0){
         return new Result(false,MessageConstant.QUERY_MEMBERMESSAGE_FAIL);
        }else {
            for (Cookie cookie : cookies) {
                String cookieName = cookie.getName();
                if (cookieName.equals("login_member_telephone")) {
                    String cookieValue = cookie.getValue();
                    memberMessage = jedisPool.getResource().get(cookieValue);

                    System.out.println( memberMessage);

                    return new Result(true, MessageConstant.QUERY_MEMBERMESSAGE_SUCCESS, memberMessage);
                }
            }
            //  request.setAttribute("memberMessage",memberMessage);
           return new Result(false, MessageConstant.QUERY_MEMBERMESSAGE_SUCCESS);
        }
    }
}

